﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Yuwell.AtomizerWeight.App.Views
{
    /// <summary>
    /// SeriesPortWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SeriesPortWindow : Window
    {
        public SeriesPortWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            CoreModule.LocalConfigurationInfo info = CoreModule.LocalConfigurationInfo.Load();
            info.PortName = this.cbPortNames.Text.Trim();
            info.Save();
            this.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            var portNames = System.IO.Ports.SerialPort.GetPortNames()
                   .OrderBy(item => item)
                   .ToList();
            this.cbPortNames.ItemsSource = portNames;

            var info = CoreModule.LocalConfigurationInfo.Load();

            if (portNames.Count == 0)
            {
                this.btnOk.IsEnabled = false;
            }
             
            if (string.IsNullOrWhiteSpace(info.PortName))
            {
                this.cbPortNames.SelectedIndex = 0;
            }
            else
            {
                if (portNames.Contains(info.PortName))
                {
                    this.cbPortNames.Text = info.PortName;
                }
                else
                {
                    this.cbPortNames.SelectedIndex = 0;
                }

            }
             
        }
    }
}
